<template>
  <div class="sign-in-view">
    <div class="content">
      <div class="calendar-container">
        <el-calendar v-model="currentDate" />
      </div>
      <div class="map-container">
        <div id="map" ref="mapContainer"></div>
      </div>
      <div class="sign-in-button-container">
        <button v-if="!signedInToday" @click="signIn">签到</button>
        <div v-else class="signed-in-text">今日已签到</div>
      </div>
    </div>
  </div>
  <div class="bottom-fixed-button">
    <el-button type="primary" style="width: 90%;" @click="returnToTechnician">返回菜单</el-button>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue';
import { useRouter } from 'vue-router';
import axios from 'axios';
import dayjs from 'dayjs';
import {ElMessage} from "element-plus";

// 引入腾讯地图的全局对象
const TMap = (window as any).TMap;

const router = useRouter();
const signedInToday = ref(false);
const currentDate = ref(dayjs().format('YYYY-MM-DD'));

onMounted(() => {
  checkSignInStatus();
  initMap();
});

const checkSignInStatus = async () => {
  try {
    const response = await axios.get('http://localhost:8002/sign-in1/check');
    signedInToday.value = response.data;
  } catch (error) {
    console.error('检查签到状态时出错:', error);
  }
};

const signIn = async () => {
  try {
    const response = await axios.post('http://localhost:8002/sign-in1/signin');
    if (response.data) {
      signedInToday.value = true;
      ElMessage.success('签到成功！')
    }
  } catch (error) {
    console.error('签到时出错:', error);
  }
};

const returnToTechnician = () => {
  router.push('/technician/home');
};

// 初始化腾讯地图
const initMap = () => {
  const map = new TMap.Map(document.getElementById("map") as HTMLElement, {
    center: new TMap.LatLng(39.984104, 116.307503), // 默认中心点（北京）
    zoom: 17, // 缩放级别
  });

  getCurrentLocation(map);
};

const getCurrentLocation = (map: any) => {
  if (navigator.geolocation) {
    ElMessage.info('正在获取您的位置...');

    navigator.geolocation.getCurrentPosition(
        position => {
          const lat = position.coords.latitude;
          const lng = position.coords.longitude;
          const userLocation = new TMap.LatLng(lat, lng);

          map.setCenter(userLocation);

          new TMap.MultiMarker({
            map: map,
            geometries: [
              {
                id: "user-location",
                position: userLocation,
                properties: {
                  title: "您的当前位置"
                }
              }
            ]
          });

          ElMessage.success('位置获取成功！');
        },
        error => {
          console.error("获取位置信息失败", error);
          ElMessage.error('无法获取您的位置，请检查定位权限或稍后再试。');
        },
        {
          enableHighAccuracy: true, // 启用高精度定位
          timeout: 10000, // 设定超时时间为10秒
          maximumAge: 0 // 不使用缓存的位置信息
        }
    );
  } else {
    ElMessage.error("浏览器不支持地理定位");
  }
};
</script>


<style scoped>
.sign-in-view {
  display: flex;
  flex-direction: column;
  background-color: white;
}

.signed-in-text {
  width: 100%;
  padding: 10px 0;
  font-size: 16px;
  text-align: center;
  border: 1px solid #dcdcdc;
  border-radius: 4px;
  background-color: #d4edda;
}

.content {
  flex: 1;
  padding: 20px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}

.map-container {
  width: 100%;
  max-width: 400px;
  height: 200px;
  margin-bottom: 20px;
}

#map {
  width: 100%;
  height: 100%;
}

.calendar-container {
  width: 100%;
  max-width: 400px;
}

.sign-in-button-container {
  width: 100%;
  max-width: 400px;
  display: flex;
  justify-content: center;
}

button {
  width: 100%;
  padding: 10px 0;
  font-size: 16px;
}

.bottom-fixed-button {
  height: 50px;
  display: flex;
  justify-content: center;
  align-items: center;
}
</style>
